package com.library.web.controller.picture;

import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.PageDomain;
import com.library.common.core.page.TableDataInfo;
import com.library.common.core.page.TableSupport;
import com.library.common.enums.BusinessType;
import com.library.common.utils.poi.ExcelUtil;
import com.library.framework.manager.BizCodeManager;
import com.library.picture.books.entity.PictureBooksInfo;
import com.library.picture.books.service.IPictureBooksInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 绘本管理Controller
 *
 * @author wanghongjie
 * @date 2022-04-26
 */
@RestController
@RequestMapping("/pictureBooks/info")
public class PictureBooksInfoController extends BaseController {
    @Autowired
    private IPictureBooksInfoService pictureBooksInfoService;
    @Resource
    private BizCodeManager bizCodeManager;

    /**
     * 查询绘本管理列表
     */
    @PreAuthorize("@ss.hasPermi('pictureBooks:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(PictureBooksInfo pictureBooksInfo) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.DESC, "updateTime"));
        List<PictureBooksInfo> list = pictureBooksInfoService.selectPictureBooksInfoList(pictureBooksInfo, pageable);
        return getDataTable(list);
    }

    /**
     * 导出绘本管理列表
     */
    @PreAuthorize("@ss.hasPermi('pictureBooks:info:export')")
    @Log(title = "绘本管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PictureBooksInfo pictureBooksInfo) {
        Pageable pageable = PageRequest.of(0, 30, Sort.by(Sort.Direction.DESC, "updateTime"));
        List<PictureBooksInfo> list = pictureBooksInfoService.selectPictureBooksInfoList(pictureBooksInfo, pageable);
        ExcelUtil<PictureBooksInfo> util = new ExcelUtil<PictureBooksInfo>(PictureBooksInfo.class);
        util.exportExcel(response, list, "绘本管理数据");
    }

    /**
     * 获取绘本管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('pictureBooks:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(pictureBooksInfoService.selectPictureBooksInfoById(id));
    }

    /**
     * 获取绘本管理详细信息
     */
    @GetMapping(value = "/pictureBookNo")
    public AjaxResult pictureBookNo(String pictureBookNo) {
        return AjaxResult.success(pictureBooksInfoService.selectPictureBooksInfoByNo(pictureBookNo));
    }

    /**
     * 更新绘本推荐信息
     */
    @GetMapping(value = "/updateRecommendBooks")
    public AjaxResult updateRecommendBooks(String pictureBookNo) {
        return AjaxResult.success(pictureBooksInfoService.updateRecommendBooks(pictureBookNo));
    }

    /**
     * 新增绘本管理
     */
    @PreAuthorize("@ss.hasPermi('pictureBooks:info:add')")
    @Log(title = "绘本管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PictureBooksInfo pictureBooksInfo) {
        String hb = bizCodeManager.createHb();
        pictureBooksInfo.setPictureBookNo(hb);
        return toAjax(pictureBooksInfoService.insertPictureBooksInfo(pictureBooksInfo));
    }

    /**
     * 修改绘本管理
     */
    @PreAuthorize("@ss.hasPermi('pictureBooks:info:edit')")
    @Log(title = "绘本管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PictureBooksInfo pictureBooksInfo) {
        return toAjax(pictureBooksInfoService.updatePictureBooksInfo(pictureBooksInfo));
    }

    /**
     * 删除绘本管理
     */
    @PreAuthorize("@ss.hasPermi('pictureBooks:info:remove')")
    @Log(title = "绘本管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(pictureBooksInfoService.deletePictureBooksInfoByIds(ids));
    }
}
